package com.zy.openchat.util;

import android.graphics.BitmapFactory;


import com.zy.openchat.Constants;

import java.io.File;

/**
 * @ClassName WeChatImageUtils
 * @Description  高仿微信的图片宽高比,获取小视频的参数
 * @Author dhl
 * @Date 2021/1/7 13:49
 * @Version 1.0
 */
public class WeChatImageUtils {

    private static final String TAG = "WeChatImageUtils";
    /**
     * 适当的宽高
     * @param outWidth
     * @param outHeight
     * @return
     */
//    public static int[] getImageSizeByOrgSizeToWeChat(int outWidth, int outHeight) {
//        int imageWidth = Constants.MEDIA_MAX_WIDTH;
//        int imageHeight = Constants.MEDIA_MAX_WIDTH;
//        int maxWidth = Constants.MEDIA_MAX_WIDTH;
//        int maxHeight = Constants.MEDIA_MAX_WIDTH;
//        int minWidth = Constants.MEDIA_MAX_WIDTH/2;
//        int minHeight = minWidth;
//        if(outWidth == 0 && outHeight == 0){
//            return new int[]{imageWidth,imageHeight};
//        }
//        if (outWidth / maxWidth > outHeight / maxHeight) {//
//            if (outWidth >= maxWidth) {//
//                imageWidth = maxWidth;
//                imageHeight = outHeight * maxWidth / outWidth;
//            } else {
//                imageWidth = outWidth ;
//                imageHeight = outHeight;
//
//            }
//            if (outHeight < minHeight) {
//                imageHeight = minHeight;
//                int width = outWidth * minHeight / outHeight;
//                if (width > maxWidth) {
//                    imageWidth = maxWidth;
//                } else {
//                    imageWidth = width;
//                }
//            }
//        } else {
//            if (outHeight >= maxHeight) {
//                imageHeight = maxHeight;
//                if (outHeight / maxHeight > 10) {
//                    imageWidth = outWidth * 5 * maxHeight / outHeight;
//                } else {
//                    imageWidth = outWidth * maxHeight / outHeight;
//                }
//
//            } else {
//                imageHeight = outHeight ;
//                imageWidth = outWidth;
//            }
//            if (outWidth < minWidth) {
//                imageWidth = minWidth ;
//                int height = outHeight * minWidth / outWidth;
//                if (height > maxHeight) {
//                    imageHeight = maxHeight;
//                } else {
//                    imageHeight = height;
//                }
//            }
//        }
//
//        return new int[]{imageWidth,imageHeight};
//    }

    public static int[] getImageSizeByOrgSizeToWeChat(int outWidth, int outHeight) {
          int imageWidth = outWidth;
          int imageHeight = outHeight;
          int maxWidth = Constants.MEDIA_MAX_WIDTH;
          int minWidth = (int)(Constants.MEDIA_MAX_WIDTH*(2/3.f));
          if(outWidth == 0 || outHeight == 0){
              return new int[]{minWidth,minWidth};
          }
          if(outWidth>maxWidth){
              imageWidth = maxWidth;
              imageHeight = (int)(imageWidth*(outHeight/(float)outWidth));
          }else if(outWidth<minWidth){
              imageWidth = minWidth;
              imageHeight = (int)(imageWidth*(outHeight/(float)outWidth));
          }
          return new int[]{imageWidth,imageHeight};
    }


//    public static int[] getVideoSizeByOrgSizeToWeChat(int outWidth, int outHeight) {
//        int imageWidth = 600;
//        int imageHeight = 600;
//        int maxWidth = 600;
//        int minWidth = 300;
//        if(outWidth == 0 || outHeight == 0){
//            return new int[]{imageWidth,imageHeight};
//        }
//        float hwRate = outHeight/(float)outWidth;
//        if(outWidth<=maxWidth){
//            if(outWidth>=minWidth){
//                imageWidth = outWidth;
//                imageHeight = outHeight;
//            }else{
//                imageWidth = minWidth;
//                imageHeight = (int)(hwRate*minWidth);
//            }
//        }else{
//            imageWidth = maxWidth;
//            imageHeight = (int)(hwRate*maxWidth);
//        }
//        return new int[]{imageWidth,imageHeight};
//    }
    /**
     * 获取图片的尺寸，只获取尺寸，不加载bitmap 不会耗时
     *
     */
    public static int[] getSize(File file) {
        if (file == null) return new int[]{0, 0};
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
        return new int[]{opts.outWidth, opts.outHeight};
    }

}
